﻿/**	@file    ProcessPic.cs
*	@note    Lianyungang Jari Electron. All Right Reserved.
*	@brief   XML解析类
*
*	@author	  Shuai Wang
*	@date	 2013/08/25
*
*	@note    下面的note和warning为可选项目
*	@note    这里填写本文件的详细功能描述和注解
*	@note    历史记录:
*	@note    V1.1.0  功能类 XML解析类。
*
*	@warning 这里填写本文件相关的警告信息
*/
using System.Xml;

namespace WHYHLWebservices
{
    public class XmlTools
    {

        /** @fn:	  CheckXmlExsitOrValid()
       *   @brief:   检查XML是否有效
       *   @param:   string _xmlFilePath 配置文件路径
       *   @param:   int nType 1为基础配置，2为其他配置
       *   @return:  bool
       */
        public static bool CheckXmlExsitOrValid(string _xmlFilePath, int nType)
        {
            try
            {
                XmlDocument doc = new XmlDocument();
                doc.Load(_xmlFilePath);
                return true;
            }
            catch /*(System.Xml.XmlException)*/
            {
                CreateDftCfgXml(_xmlFilePath);

                return false;
            }
        }

        /** @fn:	  GetConfigValue()
       *   @brief:   获取指定标签的值
       *   @param:   string _xmlFilePath 配置文件路径
       *   @param:   string key 标签
       *   @return:  bool
       */
        public static string GetConfigValue(string _xmlFilePath, string key)
        {
            try
            {
                XmlDocument doc = new XmlDocument();
                doc.Load(_xmlFilePath);
                XmlNode xmlNode = doc.SelectSingleNode("ROOT");
                return doc.DocumentElement.GetElementsByTagName(key)[0].InnerText;
            }
            catch (System.Exception)
            {
                return "";
            }
        }

        public static string[] GetDevices(string _xmlFilePath)
        {
            string[] arrValue = new string[0];
            try
            {
                XmlDocument doc = new XmlDocument();
                doc.Load(_xmlFilePath);
                XmlNode xmlNode = doc.SelectSingleNode("ROOT");
                XmlNodeList nodeList = doc.DocumentElement.GetElementsByTagName("Device");
                int nIndex = 0;
                foreach (XmlNode node in nodeList)
                {
                    arrValue[nIndex] = node.InnerText;
                    nIndex++;
                }

                return arrValue;
            }
            catch (System.Exception)
            {
                return null;
            }
        }

        /** @fn:	  SaveConfigValue()
       *   @brief:   获取指定标签的值
       *   @param:   string _xmlFilePath 配置文件路径
       *   @param:   string key 标签
       *   @param:   string value 值
       *   @return:  bool
       */
        public static bool SaveConfigValue(string _xmlFilePath, string key, string value)
        {
            try
            {
                XmlDocument doc = new XmlDocument();
                doc.Load(_xmlFilePath);
                doc.DocumentElement.GetElementsByTagName(key)[0].InnerText = value;
                doc.Save(_xmlFilePath);
                return true;
            }
            catch
            {
                return false;
            }
        }

        public static bool SaveDevice(string _xmlFilePath, string devIP)
        {
            try
            {
                XmlDocument doc = new XmlDocument();
                doc.Load(_xmlFilePath);

                XmlNode root = doc.SelectSingleNode("ROOT");
                XmlElement device = doc.CreateElement("Device");
                device.SetAttribute("IP", devIP);
                root.AppendChild(device);

                doc.Save(_xmlFilePath);
                return true;
            }
            catch
            {
                return false;
            }
        }

        public static bool CheckDeviceExsit(string _xmlFilePath, string devIP)
        {
            try
            {
                XmlDocument doc = new XmlDocument();
                doc.Load(_xmlFilePath);

                XmlNode root = doc.SelectSingleNode("ROOT");
                XmlNodeList nodeList = doc.DocumentElement.GetElementsByTagName("Device");
                foreach (XmlNode node in nodeList)
                {
                    if (node.Attributes["IP"].Value == devIP)
                    {
                        return true;
                    }
                }

                return false;
            }
            catch
            {
                return false;
            }
        }

        public static bool DelDevice(string _xmlFilePath, string devIP)
        {
            try
            {
                XmlDocument doc = new XmlDocument();
                doc.Load(_xmlFilePath);

                XmlNode root = doc.SelectSingleNode("ROOT");
                XmlNodeList nodeList = doc.DocumentElement.GetElementsByTagName("Device");
                foreach (XmlNode node in nodeList)
                {
                    if (node.Attributes["IP"].Value == devIP)
                    {
                        root.RemoveChild(node);
                        break;
                    }
                }

                doc.Save(_xmlFilePath);
                return true;
            }
            catch
            {
                return false;
            }
        }

        /** @fn:	  CreateDftCfgXml()
       *   @brief:   创建其他参数配置文件
       *   @param:   string _xmlFilePath 配置文件路径
       *   @return:  bool
       */
        public static bool CreateDftCfgXml(string _xmlFilePath)
        {
            XmlDocument doc = new XmlDocument();
            XmlDeclaration declare = doc.CreateXmlDeclaration("1.0", "UTF-8", "yes");
            doc.AppendChild(declare);
            XmlElement root = doc.CreateElement("ROOT");
            doc.AppendChild(root);
            XmlElement device = doc.CreateElement("Device");
            device.SetAttribute("IP", "192.168.1.100");
            root.AppendChild(device);

            doc.Save(_xmlFilePath);
            return true;
        }

        /** @fn:	  GetXmlAttribute()
        *   @brief:   获取节点属性
        *   @param:   XmlNode xmlNode xml节点
        *   @param:  string xmlAttributeName 属性名称
        *   @return:  string
        */
        public static string GetXmlAttribute(XmlNode xmlNode, string xmlAttributeName)
        {
            string strAttrib = string.Empty;
            try
            {
                if (null != xmlNode)
                {
                    if (xmlNode.Attributes.Count > 0)
                    {
                        XmlAttribute xmlAttrbut = xmlNode.Attributes[xmlAttributeName];
                        if (null != xmlAttrbut)
                        {
                            return xmlAttrbut.Value;
                        }
                    }
                }
            }
            catch (System.Exception)
            {
            }
            return strAttrib;
        }
    }  
}